package com.ld.hpfs.web.controller;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
//import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
//import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.ld.hpfs.common.jackson.JackJson;
//import com.ld.hpfs.pojo.ExtPager;
//import com.ld.hpfs.dao.UtilMapper;
import com.ld.hpfs.pojo.User;
import com.ld.hpfs.service.HistoryService;
import com.ld.hpfs.service.TicketTypeService;
import com.ld.hpfs.web.interseptor.WebConstants;

@Controller
public class TicketTypeController {

	private static final Logger logger = LoggerFactory
			.getLogger(TicketTypeController.class);
	@Autowired
	private TicketTypeService tickettypeService;

	// 用于history表
	@Autowired
	private HistoryService historyService;

	// @Autowired
	// private UtilMapper utilMapper;
	/**
	 * 票据类型设置
	 * 
	 * @author Administrator
	 */
	@RequestMapping(value = "/TicketInvoiceSetting")
	public void TicketInvoiceSetting(@RequestParam String WriteBackWay,
			@RequestParam String InterfaceWay, @RequestParam String type,
			@RequestParam String WriteBackERPWay,
			@RequestParam String DataImportWay, @RequestParam String PriceWay,
			@RequestParam String ImportCalaRuler,
			@RequestParam String MaxMoney, @RequestParam String MaxLine,
			@RequestParam String UnitPriceCalaAcc,
			@RequestParam String NumCalaAcc, @RequestParam String SplitCalaAcc,
			@RequestParam String SplitCalaStep, @RequestParam String MinMoney,
			@RequestParam String SplitResultLimit,
			@RequestParam String isOutCode,
			@RequestParam String isDisplayPinAccount,
			@RequestParam String isUseMdataClient,
			@RequestParam String isUseMdataGoods,
			@RequestParam String OneTicketMakeSetting,
			@RequestParam String ImportOverrideAppend,
			@RequestParam String PriceOrCount,
			@RequestParam String AbandonCheck, HttpSession session,
			HttpServletRequest request, HttpServletResponse response,
			PrintWriter writer) {
		HashMap<String, Object> result = new HashMap<String, Object>();
		User user = (User) session.getAttribute(WebConstants.CURRENT_USER);
		if (user == null) {
			result.put("resultcode", -99);
		}
		HashMap<String, Object> param = new HashMap<String, Object>();
		response.setContentType("text/plain;charset=UTF-8");
		try {
			Object currentCompany = session
					.getAttribute(WebConstants.CURRENT_COMPANY);
			String company_id = currentCompany.toString();
			param.put("company_id", company_id);
			param.put("WriteBackWay", WriteBackWay);
			param.put("InterfaceWay", InterfaceWay);
			param.put("WriteBackERPWay", WriteBackERPWay);
			param.put("DataImportWay", DataImportWay);
			param.put("PriceWay", PriceWay);
			param.put("ImportCalaRuler", ImportCalaRuler);
			param.put("MaxLine", MaxLine);
			param.put("UnitPriceCalaAcc", UnitPriceCalaAcc);
			param.put("NumCalaAcc", NumCalaAcc);
			param.put("SplitCalaAcc", SplitCalaAcc);
			param.put("SplitResultLimit", SplitResultLimit);
			param.put("isOutCode", isOutCode);
			param.put("isDisplayPinAccount", isDisplayPinAccount);
			param.put("isUseMdataClient", isUseMdataClient);
			param.put("isUseMdataGoods", isUseMdataGoods);
			param.put("type", type);
			param.put("OneTicketMakeSetting", OneTicketMakeSetting);
			param.put("ImportOverrideAppend", ImportOverrideAppend);
			param.put("PriceOrCount", PriceOrCount);
			param.put("AbandonCheck", AbandonCheck);
			if (MaxMoney != "") {
				param.put("MaxMoney", MaxMoney);
			} else {
				int maxmoney = 0;
				param.put("MaxMoney", maxmoney);
			}
			if (MinMoney != "") {
				param.put("MinMoney", MinMoney);
			} else {
				int minmoney = 0;
				param.put("MinMoney", minmoney);
			}
			if (SplitCalaStep != "") {
				param.put("SplitCalaStep", SplitCalaStep);
			} else {
				int splitcalastep = 0;
				param.put("SplitCalaStep", splitcalastep);
			}

			// 用于添加history表
			HashMap<String, Object> hist_param = new HashMap<String, Object>();
			hist_param.put("type", type);
			hist_param.put("company_id", company_id);
			hist_param.put("update_id", Integer.parseInt(user.getUserId()));
			hist_param.put("update_name", user.getAccount());
			historyService.addTicketHistory(hist_param, true);

			int ret = tickettypeService.TicketInvoiceSetting(param);
			result.put("resultcode", ret);
			writer.print(JackJson.fromObjectToJson(result));
		} catch (Exception e) {
			logger.error("Exception: ", e);
		} finally {
			writer.flush();
			writer.close();
		}
	}

	/**
	 * 票据类型设置
	 * 
	 * @author Administrator
	 */
	@RequestMapping(value = "/getInvoiceData")
	@ResponseBody
	public void getInvoiceData(@RequestParam String type, HttpSession session,
			HttpServletRequest request, HttpServletResponse response,
			PrintWriter writer) {
		HashMap<String, Object> param = new HashMap<String, Object>();
		try {
			Object currentCompany = session
					.getAttribute(WebConstants.CURRENT_COMPANY);
			String company_id = currentCompany.toString();
			param.put("company_id", company_id);
			param.put("type", type);
			int VaildCompany = tickettypeService.VaildCompany(param);
			if (VaildCompany < 1) {
				param.put("company_id", "-1");
			}
			ArrayList<HashMap<String, Object>> result = tickettypeService
					.getInvoiceData(param);
			String returnMsg = JackJson.fromObjectToJson(result);
			logger.debug("{}", returnMsg);
			writer.print(returnMsg);
		} catch (Exception e) {
			logger.error("Exception: ", e);
		} finally {
			writer.flush();
			writer.close();
		}
	}

	@RequestMapping(value = "/checkAbandon")
	@ResponseBody
	public void checkAbandon(HttpSession session, HttpServletRequest request,
			HttpServletResponse response, PrintWriter writer) {
		HashMap<String, Object> param = new HashMap<String, Object>();
		String returnMsg = "true";
		try {
			Object currentCompany = session
					.getAttribute(WebConstants.CURRENT_COMPANY);
			String company_id = currentCompany.toString();
			String ticketType = request.getParameter("TicketType");
			String abandonCheck = request.getParameter("AbandonCheck")==""?"0":request.getParameter("AbandonCheck");

			// 放入参数
			param.put("company_id", company_id);
			param.put("ticketType", ticketType);
			int checkLevel = tickettypeService.checkLevel(param);
			// 如果不一致
			if (checkLevel != Integer.parseInt(abandonCheck)) {
				param.put("abandonCheck", abandonCheck);
				int checkAbandon = tickettypeService.checkAbandon(param);
				if (checkAbandon > 0){
					returnMsg = "false";
				}
			}
			writer.print(returnMsg);
		} catch (Exception e) {
			logger.error("Exception: ", e);
		} finally {
			writer.flush();
			writer.close();
		}

	}
}
